package com.yidu.logisticssystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.dao.LogisticsMapDao;
import com.yidu.logisticssystem.dao.LogisticsSiteDao;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsSite;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.LogisticsState;
import com.yidu.logisticssystem.dao.LogisticsStateDao;
import com.yidu.logisticssystem.entity.vo.StateVo;
import com.yidu.logisticssystem.service.LogisticsStateService;
import com.yidu.logisticssystem.utils.BaseMessage;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货运状态(LogisticsState)表服务实现类
 *
 * @author lzx
 * @since 2021-03-08 16:46:57
 */
@Service("logisticsStateService")
public class LogisticsStateServiceImpl implements LogisticsStateService {
    @Resource
    private LogisticsStateDao logisticsStateDao;

    @Resource
    private LogisticsMapDao mapDaol;

    @Resource
    private LogisticsSiteDao selectidmap;

    @Override
    public int updateWay(LogisticsState logisticsState) {
        return logisticsStateDao.updateWay(logisticsState);
    }

    /**
     * 根据货运订单查询已完成的状态
     * @param state
     * @return
     */
    @Override
    public List<LogisticsState> findOk(LogisticsState state) {
        return logisticsStateDao.findOk(state);
    }

    /**
     * 根据货运订单查询当前状态
     * @param state
     * @return
     */
    @Override
    public List<LogisticsState> findNo(LogisticsState state) {
        return logisticsStateDao.findNo(state);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param stateId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsState queryById(Integer stateId) {
        return this.logisticsStateDao.queryById(stateId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsState> queryAllByLimit(int offset, int limit) {
        return this.logisticsStateDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logisticsState 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(LogisticsState logisticsState) {
        return this.logisticsStateDao.insert(logisticsState);
    }

    /**
     * 修改数据
     *
     * @param logisticsState 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsState update(LogisticsState logisticsState) {
        this.logisticsStateDao.update(logisticsState);
        return this.queryById(logisticsState.getStateId());
    }

    /**
     * 通过主键删除数据
     *
     * @param stateId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer stateId) {
        return this.logisticsStateDao.deleteById(stateId) > 0;
    }

    @Override
    public List<StateVo> selecthyd(StateVo vo) {
        return logisticsStateDao.selecthyd(vo);
    }

    @Override
    public List<StateVo> selectfahuo(StateVo areaId) {
        return logisticsStateDao.selectfahuo(areaId);
    }

    @Override
    public int updatefahuo(String[] id) {
        return logisticsStateDao.updatefahuo(id);
    }

    @Override
    public Map<String, Object> selectshdz(StateVo vo, HttpServletRequest request) {
        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");//取得staff 登录对象
        vo.setAreaId(staff.getMapId()); //将地区放入 vo
        //将参数传入分页助手 PageHelper
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        Map<String,Object> map = new HashMap<>();
        List<StateVo> list =logisticsStateDao.selectshdz(vo);//搜索收货地址
        for (StateVo stateVo:list) {
            int dz2=stateVo.getWayConsignarea().indexOf(",");//取到收货地址位置
            stateVo.setWayConsignarea(stateVo.getWayConsignarea().substring(dz2+1));//截取地址后面的纤细地址
        }
        PageInfo<StateVo> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);//所有数据
        map.put("count",pageInfo.getTotal());//总行数
        return map;
    }



    @Override
    public Map<String, Object> selectkdyps(StateVo vo,HttpServletRequest request) {
        LogisticsStaff staff = (LogisticsStaff)request.getSession().getAttribute("staff");
        int areaid=staff.getStaffId(); //地区编号
        System.out.println(areaid);
        //分页 PageHelper
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        Map<String,Object> map = new HashMap<>();
        List<StateVo> list =logisticsStateDao.selectkdyps(areaid);//快递员派送
        for (StateVo stateVo:list) {
            int dz2=stateVo.getWayConsignarea().indexOf(",");//收货货地址
            LogisticsMap map2=mapDaol.queryById(stateVo.getWayConsignarea().substring(0,dz2));//根据map id  0 - dz2
            stateVo.setWayConsignarea(map2.getMapName()+stateVo.getWayConsignarea().substring(dz2+1)); //页面显示  + 后面详细地址
        }
        PageInfo<StateVo> pageInfo = new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);//所有数据
        map.put("count",pageInfo.getTotal());//总行数
        return map;
    }






}
